package com.zhongxi.guoyuan.adapter.ad;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.view.beautifulStore.StoreJDActivity;
import com.zhongxi.guoyuan.view.rewardCoin.HotRewardCoinActivity;
import com.zhongxi.guoyuan.widget.ad.JDAdverView;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2016/10/26.
 */

public class JDViewAdapter {

    private Context context;
    private ArrayList<HotGoodsData> mDatas;
    private int type;
    private ArrayList<ProjectItemData> projectItemDatas;

    public JDViewAdapter(Context context, ArrayList<HotGoodsData> mDatas, int type) {
        this.context = context;
        this.mDatas = mDatas;
        if (mDatas == null || mDatas.isEmpty()) {
            throw new RuntimeException("nothing to show");
        }
        this.type = type;
    }


    /**
     * 获取数据的条数
     *
     * @return
     */
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    /**
     * 获取某个数据
     *
     * @param position
     * @return
     */
    public HotGoodsData getItem(int position) {
        return mDatas.get(position);
    }

    /**
     * 获取条目布局
     *
     * @param parent
     * @return
     */
    public View getView(JDAdverView parent) {
        View view = null;
        switch (type) {
            case 0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_item, null);
                break;
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_reward_item, null);
                break;
        }
        return view;
    }

    /**
     * 条目数据适配
     *
     * @param view
     * @param data
     */
    public void setItem(final View view, final HotGoodsData data) {
        try {
            if (0 == type) {
                TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
                TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
                TextView tvcoin = (TextView) view.findViewById(R.id.tvcoin);
                TextView tvCash = (TextView) view.findViewById(R.id.tvCash);
                TextView tvOrign = (TextView) view.findViewById(R.id.tvOrign);
                ImageView ivImg = (ImageView) view.findViewById(R.id.ivImg);
                if (null != data.getThumbnail() && !"".equals(data.getThumbnail())) {
                    Glide.with(context).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_loading).fitCenter().into(ivImg);
                }
                if (null != data.getName() && !"".equals(data.getName())) {
                    tvTitle.setText(data.getName());
                }
                if (null != data.getStatus() && !"".equals(data.getStatus())) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String time = sdf.format(new Date(Long.parseLong(data.getStatus()) * 1000));
                    tvTime.setText(time);
                }
                if (null != data.getPrice() && !"".equals(data.getPrice())) {
                    tvCash.setText(data.getPrice() + " 元");
                } else {
                    tvCash.setText("0 元");
                }

            } else if (type == 1) {
                TextView tvCoinTitle = (TextView) view.findViewById(R.id.tvCoinTitle);
//                TextView tvShopName = (TextView) view.findViewById(R.id.tvShopName);
                TextView tvCoin = (TextView) view.findViewById(R.id.tvCoin);
                TextView tvBack = (TextView) view.findViewById(R.id.tvBack);
                TextView tv_yield = (TextView) view.findViewById(R.id.tv_yield);
                ImageView ivStoreImg = (ImageView) view.findViewById(R.id.ivStoreImg);
                if (null != data.getThumbnail() && !"".equals(data.getThumbnail())) {
//                    Glide.with(context).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(ivStoreImg);
                    Glide.with(context).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_loading).fitCenter().into(ivStoreImg);
                }
                if (null != data.getName() && !"".equals(data.getName())) {
                    tvCoinTitle.setText(data.getName().trim());
                }
//                if (null != data.getSubject() && !"".equals(data.getSubject())) {
//                    tvShopName.setText(data.getSubject().trim());
//                }
                if (null != data.getPrice() && !"".equals(data.getPrice())) {
                    tvCoin.setText(data.getPrice() + "元");
                } else {
                    tvCoin.setText("0元");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //你可以增加点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //比如打开url
                Intent intent = new Intent();
                switch (type) {
                    case 0:
                        intent.setClass(context, StoreJDActivity.class);
                        intent.putExtra("datas", mDatas);
                        context.startActivity(intent);
                        break;
                    case 1:
                        intent.setClass(context, HotRewardCoinActivity.class);
                        intent.putExtra("datas", mDatas);
                        intent.putExtra("category",projectItemDatas);
                        context.startActivity(intent);
                        break;
                }
            }
        });
    }

    public void setTitleData(ArrayList<ProjectItemData> projectItemDatas) {
        this.projectItemDatas = projectItemDatas;
    }
}
